package cn.bigfire.wzdj.util;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.bigfire.wzdj.pojo.bo.WZDJPack;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Base64;
import java.util.List;

@Slf4j
public class HexAnalyzeJFrame {


    public static void main(String[] args) {
        // 创建 JFrame
        JFrame frame = new JFrame("抓包数据分析");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(600, 100, 800, 600);
        frame.setLayout(new GridLayout(2, 1));

        // 创建第一个面板，包含 JTextArea 和 JButton
        JPanel panel1 = new JPanel(new BorderLayout());
        JTextArea textArea1 = new JTextArea();
        textArea1.setFont(new Font("Serif", Font.PLAIN, 16));
        JScrollPane scrollPane1 = new JScrollPane(textArea1);
        panel1.add(scrollPane1, BorderLayout.CENTER);

        JButton parseButton1 = new JButton("发送消息分析");
        panel1.add(parseButton1, BorderLayout.SOUTH);

        // 创建第二个面板，包含 JTextArea 和 JButton
        JPanel panel2 = new JPanel(new BorderLayout());
        JTextArea textArea2 = new JTextArea();
        textArea2.setFont(new Font("Serif", Font.PLAIN, 16));
        JScrollPane scrollPane2 = new JScrollPane(textArea2);
        panel2.add(scrollPane2, BorderLayout.CENTER);

        JButton parseButton2 = new JButton("响应数据分析");
        panel2.add(parseButton2, BorderLayout.SOUTH);

        // 将面板添加到 JFrame
        frame.add(panel1);
        frame.add(panel2);

        // 为按钮添加事件监听器
        parseButton1.addActionListener(e -> {
            String text = textArea1.getText();
            String trim = StrUtil.trim(text);
            log.info(ReqHexGen.genCode(trim));
        });

        parseButton2.addActionListener(e -> {
            String text = textArea2.getText();
            String trim = StrUtil.trim(text);
            byte[] bytes = Base64.getDecoder().decode(trim);
            List<WZDJPack<?>> packList = PackUtil.decodeBytes(bytes);
            for (WZDJPack<?> wzdjPack : packList) {
                log.info(wzdjPack.toString());
            }
//            WZDJPack wzdjPack = WZDJPack.parse(bytes);
//            log.info(wzdjPack);
        });

        // 显示 JFrame
        frame.setVisible(true);
    }


    @Test
    @SneakyThrows
    public void test() {
        String base64 = "AQBAAAQAEwAAAAAA";
        byte[] bytes = Base64.getDecoder().decode(base64);
        String s = HexUtil.encodeHexStr(bytes);
        log.info(s);
    }



}
